using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Voodoo.Sauce.Internal;

namespace Voodoo.Sauce.Debugger
{
	public static class LogsTracker
	{
		private static Queue<LogMessage> _issuesQueue = new Queue<LogMessage>();

		public static IReadOnlyCollection<LogMessage> Issues => _issuesQueue;

		public static int Count => _issuesQueue?.Count ?? 0;

		public static event Action<LogMessage> OnLogReceived;

		[RuntimeInitializeOnLoadMethod]
		public static void Initialize()
		{
			VoodooLog.logReceived += LogReceived;
		}

		private static void LogReceived(string message, string trace, LogType logType)
		{
			LogMessage logMessage = new LogMessage(message, trace, logType);
			_issuesQueue.Enqueue(logMessage);
			LogsTracker.OnLogReceived?.Invoke(logMessage);
		}

		public static void Clear()
		{
			_issuesQueue.Clear();
		}

		public static int CountFor(LogType logType)
		{
			return _issuesQueue?.Count((LogMessage message) => message.logType == logType) ?? 0;
		}
	}
}
